﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace RobotEscape.Screens
{
    public class PauseScreen : ScreenBase
    {
        GameScreen Parent;
        Rectangle pauseRect = new Rectangle();

        MenuItems currentItem = MenuItems.Resume;
        public PauseScreen(GameScreen parent)
        {
            Parent = parent;
        }

        public override void Init()
        {
            Canvas.SetLeft(pauseRect, 50);
            Canvas.SetTop(pauseRect, 50);
            pauseRect.Width = 300;
            pauseRect.Height = 500;
            Game.Instance.Canvas.Children.Add(pauseRect);

            base.Init();
        }

        public override void Update(GameTime time)
        {
            ProcessKeys();
            Paint();
            base.Update(time);
        }

        private void ProcessKeys()
        {
            if (KeyState.GetKeyState(Key.Down) && !KeyState.GetOldKeyState(Key.Down))
                currentItem++;
            if (KeyState.GetKeyState(Key.Up) && !KeyState.GetOldKeyState(Key.Up))
                currentItem--;

            if (currentItem < MenuItems.Resume) currentItem = MenuItems.Help;
            if (currentItem > MenuItems.Help) currentItem = MenuItems.Resume;

            if (KeyState.GetKeyState(Key.Escape) && !KeyState.GetOldKeyState(Key.Escape))
            {
                Game.Instance.SwitchScreen(this.Parent); // resume
            }

            if (KeyState.GetKeyState(Key.Enter) && !KeyState.GetOldKeyState(Key.Enter))
            {
                if (currentItem == MenuItems.Resume)
                {
                    Game.Instance.SwitchScreen(this.Parent);
                }
                if (currentItem == MenuItems.Restart)
                {
                    int level = this.Parent.levelNum;
                    this.Parent.Remove();
                    Game.Instance.SwitchScreen(new GameScreen(level));
                }
                if (currentItem == MenuItems.Help)
                {
                    Game.Instance.SwitchScreen(new HelpScreen(this.Parent));
                }
            }
        }

        private void Paint()
        {
            WriteableBitmap wb = new WriteableBitmap((int)pauseRect.Width, (int)pauseRect.Height);
            wb.Invalidate();
            wb.FillRectangle(0, 0, wb.PixelWidth, wb.PixelHeight, Util.BackgroundGrey);
            Util.DrawText(wb, "GAME PAUSED", 20, wb.PixelWidth / 2, 10, Colors.White, TextAlignment.Center);

            string txt;

            // Draw Resume menu item
            if (currentItem == MenuItems.Resume)
                txt = ">RESUME<";
            else
                txt = "RESUME";
            Util.DrawText(wb, txt, 20, wb.PixelWidth / 2, 100, Util.RobotRed, TextAlignment.Center, true);

            // Draw Restart menu item
            if (currentItem == MenuItems.Restart)
                txt = ">RESTART LEVEL<";
            else
                txt = "RESTART LEVEL";
            Util.DrawText(wb, txt, 20, wb.PixelWidth / 2, 150, Util.RobotRed, TextAlignment.Center, true);

            if (currentItem == MenuItems.Help)
                txt = ">HELP<";
            else
                txt = "HELP";
            Util.DrawText(wb, txt, 20, wb.PixelWidth / 2, 200, Util.RobotRed, TextAlignment.Center, true);

            wb.Invalidate();

            var imageBrush = new ImageBrush
            {
                Stretch = Stretch.None,
                AlignmentX = AlignmentX.Left,
                AlignmentY = AlignmentY.Top
            };
            imageBrush.ImageSource = wb;
            this.pauseRect.Fill = imageBrush;
        }

        public override void Remove()
        {
            if (Game.Instance.Canvas.Children.Contains(pauseRect))
                Game.Instance.Canvas.Children.Remove(pauseRect);
            base.Remove();
        }

        enum MenuItems
        {
            Resume = 0,
            Restart = 1,
            Help = 2
        }
    }
}
